home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.awt.LayoutManager;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class FrameSetLayout implements LayoutManager {
- static final int HORIZONTAL = 0;
- static final int VERTICAL = 1;
- private int _orientation = 0;
- private Hashtable _hash = new Hashtable();
- private int _lastWidth = -1;
- private int _lastHeight = -1;
- private int[] pctAtts;
- private int[] wildAtts;
- private int[] pixAtts;
- private boolean _bAlwaysConstraintLayout = false;
-
- public void addLayoutComponent(Component comp, Object constraints) {
- this._hash.put(comp, constraints);
- }
-
- public void addLayoutComponent(String name, Component comp) {
- }
-
- private void constraintLayout(Container parent) {
- Insets insets = parent.getInsets();
- Dimension dim = ((Component)parent).getSize();
- int top = insets.top;
- int bottom = dim.height - insets.bottom;
- int left = insets.left;
- int right = dim.width - insets.right;
- int space = this._orientation == 0 ? right - left : bottom - top;
- Component[] comps = parent.getComponents();
- Vector temp = new Vector();
- int nSplitBars = 0;
- int splitBarSpace = 0;
-
- for(int i = 0; i < comps.length; ++i) {
- if (!(comps[i] instanceof FrameSplitterBar)) {
- temp.addElement(comps[i]);
- } else {
- int splitBarWidth = ((FrameSplitterBar)comps[i]).getBarSize();
- splitBarSpace += splitBarWidth;
- ((FrameSplitterBar)comps[i]).setOrientation(this._orientation);
- if (this._orientation == 0) {
- comps[i].setBounds(left, top, splitBarWidth, dim.height);
- } else {
- comps[i].setBounds(left, top, dim.width, splitBarWidth);
- }
-
- ++nSplitBars;
- }
- }
-
- space = Math.max(0, space - splitBarSpace);
- int nFrames = temp.size();
- Component[] frames = new Component[nFrames];
- temp.copyInto(frames);
- int pixSpace = this.getPixelSpace(frames);
- int pctSpace = this.getPercentSpace(frames, space);
- this.getWildcardDivisions(frames);
- int wildcardSpace = Math.max(0, space - pctSpace - pixSpace);
-
- for(int i = 0; i < frames.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
- if (c != null && c.type == 1) {
- int sz = (int)(c.percent * (float)space);
- if (this._orientation == 0) {
- frames[i].setBounds(0, 0, sz, dim.height);
- } else {
- frames[i].setBounds(0, 0, dim.width, sz);
- }
- }
- }
-
- for(int i = 0; i < frames.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
- if (c != null && c.type == 0) {
- if (this._orientation == 0) {
- frames[i].setBounds(0, 0, c.pixels, dim.height);
- } else {
- frames[i].setBounds(0, 0, dim.width, c.pixels);
- }
- }
- }
-
- int leftOverSpace = wildcardSpace;
- int nDivisions = this.getWildcardDivisions(frames);
- int divisionWidth = 0;
- if (nDivisions > 0) {
- divisionWidth = wildcardSpace / nDivisions;
-
- for(int i = 0; i < frames.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
- if (c != null && c.type == 2) {
- int sz = c.wildcard * divisionWidth;
- if (this._orientation == 0) {
- frames[i].setBounds(0, 0, sz, dim.height);
- } else {
- frames[i].setBounds(0, 0, dim.width, sz);
- }
- }
- }
- } else if (wildcardSpace > 0) {
- if (wildcardSpace < nFrames) {
- for(int i = 0; i < leftOverSpace; ++i) {
- Rectangle b = frames[i].getBounds();
- if (this._orientation == 0) {
- frames[i].setBounds(0, 0, b.width + 1, dim.height);
- } else {
- frames[i].setBounds(0, 0, dim.width, b.height + 1);
- }
- }
- } else {
- double pct = (double)1.0F / (double)nFrames;
- int runningTotal = 0;
- int addAmount = 0;
-
- for(int i = 0; i < nFrames; ++i) {
- if (i == nFrames - 1) {
- addAmount = leftOverSpace - runningTotal;
- } else {
- addAmount = (int)(pct * (double)((float)leftOverSpace));
- }
-
- runningTotal += addAmount;
- Rectangle b = frames[i].getBounds();
- if (this._orientation == 0) {
- frames[i].setBounds(0, 0, b.width + addAmount, dim.height);
- } else {
- frames[i].setBounds(0, 0, dim.width, b.height + addAmount);
- }
- }
- }
- }
-
- int x = left;
- int y = top;
-
- for(int i = 0; i < comps.length; ++i) {
- comps[i].setLocation(x, y);
- if (this._orientation == 0) {
- x += comps[i].getBounds().width;
- } else {
- y += comps[i].getBounds().height;
- }
- }
-
- this._lastWidth = dim.width;
- this._lastHeight = dim.height;
- }
-
- void doConstraintLayout() {
- this._lastWidth = -1;
- this._lastHeight = -1;
- }
-
- public float getLayoutAlignmentX(Container target) {
- return 0.5F;
- }
-
- public float getLayoutAlignmentY(Container target) {
- return 0.5F;
- }
-
- private int getPercentSpace(Component[] comps, int space) {
- int pctSpace = 0;
-
- for(int i = 0; i < comps.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
- if (c != null && c.type == 1) {
- pctSpace = (int)((float)pctSpace + (float)space * c.percent);
- }
- }
-
- return pctSpace;
- }
-
- private int getPixelSpace(Component[] comps) {
- int total = 0;
-
- for(int i = 0; i < comps.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
- if (c != null && c.type == 0) {
- total += c.pixels;
- }
- }
-
- return total;
- }
-
- private int getWildcardDivisions(Component[] comps) {
- int nDivisions = 0;
-
- for(int i = 0; i < comps.length; ++i) {
- FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
- if (c != null && c.type == 2) {
- nDivisions += c.wildcard;
- }
- }
-
- return nDivisions;
- }
-
- public void invalidateLayout(Container target) {
- }
-
- public void layoutContainer(Container parent) {
- if (!this._bAlwaysConstraintLayout && (this._orientation != 0 || this._lastWidth != -1) && (this._orientation != 1 || this._lastHeight != -1)) {
- this.splitBarLayout(parent);
- } else {
- this.constraintLayout(parent);
- }
-
- }
-
- public Dimension maximumLayoutSize(Container target) {
- return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
- }
-
- public Dimension minimumLayoutSize(Container parent) {
- return ((Component)parent).getSize();
- }
-
- public Dimension preferredLayoutSize(Container parent) {
- return ((Component)parent).getSize();
- }
-
- public void removeLayoutComponent(Component comp) {
- }
-
- void setAlwaysConstraintLayout(boolean bAlways) {
- this._bAlwaysConstraintLayout = bAlways;
- }
-
- void setConstraints(Component c, FrameConstraint constraints) {
- this._hash.put(c, constraints);
- }
-
- void setOrientation(int orientation) {
- this._orientation = orientation;
- }
-
- private void splitBarLayout(Container parent) {
- Insets insets = parent.getInsets();
- Dimension dim = ((Component)parent).getSize();
- int top = insets.top;
- int bottom = dim.height - insets.bottom;
- int left = insets.left;
- int right = dim.width - insets.right;
- int var10000 = right - left;
- Component[] comps = parent.getComponents();
- int nFrames = 0;
-
- for(int i = 0; i < comps.length; ++i) {
- if (!(comps[i] instanceof FrameSplitterBar)) {
- ++nFrames;
- }
- }
-
- for(int i = 0; i < comps.length; ++i) {
- Component c = comps[i];
- if (c instanceof FrameSplitterBar) {
- Rectangle r = c.getBounds();
- int sz = ((FrameSplitterBar)c).getBarSize();
- if (this._orientation == 0) {
- c.setBounds(r.x, top, sz, dim.height);
- } else {
- c.setBounds(left, r.y, dim.width, sz);
- }
- } else if (i + 1 < comps.length && comps[i + 1] instanceof FrameSplitterBar) {
- int barWidth = ((FrameSplitterBar)comps[i + 1]).getBarSize();
- Point splitterPos = comps[i + 1].getLocation();
- if (this._orientation == 0) {
- int frameWidth = splitterPos.x - left;
- c.setBounds(left, top, frameWidth, dim.height);
- left += frameWidth + barWidth;
- } else {
- int frameHeight = splitterPos.y - top;
- c.setBounds(left, top, dim.width, frameHeight);
- top += frameHeight + barWidth;
- }
- } else if (i == comps.length - 1) {
- if (this._orientation == 0) {
- c.setBounds(left, top, right - left, dim.height);
- } else {
- c.setBounds(left, top, dim.width, bottom - top);
- }
- }
- }
-
- }
- }
-